export default function () {
    angular.module('ui.form')
        .directive('uiFormDate', function (eventHelper) {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                controller:function ($scope) {
                },
                require:"^?uiForm",
                scope:{
                    value:'@' //自动绑定，传递的是字符串
                },
                template: function(el,attr){
                    const key=attr.key||attr.name;
                    return '<div class={{ngClass}}> ' +
                        '<div class={{"col-md-"+cols}}>' +
                        '<label class="control-label">'+attr.label+'</label>' +
                        '</div>'+
                        '<div class="col-md-6">' +
                            '<input class="form-control" name="'+attr.name+'" type="text"  id="datetimepicker_'+key+'"readonly>'+
                        '</div>'+
                        '</div>';
                },
                link : function(scope, element, attrs,ctrl,transcludeFn) {

                    const datepicker=$(element).find("input").datetimepicker({
                        format: attrs.format||'yyyy-mm-dd hh:ii:ss',
                        autoclose: true,
                        todayBtn: true,
                        pickerPosition: "bottom-left"
                    });
                    const result=['has-success','has-error','has-success'];
                    const cols=ctrl._spliceColumn()||2;
                    scope.ngClass="form-group col-md-"+12/cols;
                    scope.cols=parseInt(cols)+1;
                    scope.$on("ngValueReset",function (event) {
                        datepicker.datetimepicker('update',scope.value);
                    });
                    scope.$watch('value',function (newValue,oldValue) {
                        datepicker.datetimepicker('update',newValue);
                    });
                },
            };
        });
}